package com.obixlabs.security.credentialsstore.model;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class UserGroups implements Serializable
{
	private static final long serialVersionUID = -1760627449005052729L;
	
	private Map<String, ObixGroup> currentMemberShips;
	private Set<ObixGroup> newMemberships;
	private Set<ObixGroup> discontinuedMemberships;
	
	protected UserGroups()
	{
		currentMemberShips = new HashMap<String, ObixGroup>();
		newMemberships = new HashSet<ObixGroup>();
		discontinuedMemberships = new HashSet<ObixGroup>();
	}
	
	public UserGroups(ObixGroups groups)
	{
		this();
		currentMemberShips.putAll(groups);
	}
	
	protected boolean leave(ObixGroup group) 
	{
		boolean result = true;
		if (currentMemberShips.containsKey(group.getName()))
			discontinuedMemberships.add(group);	
		else result = false;
		return result;
	}
	
	protected boolean join(ObixGroup group) 
	{
		boolean result = true;
		if (!currentMemberShips.containsKey(group.getName()))			
			newMemberships.add(group);
		else result = false;
		return result;
	}
		
	public boolean isChanged() 
	{
		boolean result;
		if (!newMemberships.isEmpty() || !discontinuedMemberships.isEmpty())
			result = true;
		else result = false;
		return result;
	}
	
	public Map<String, ObixGroup> getCurrentMemberShips() 
	{return Collections.unmodifiableMap(currentMemberShips);}
	
	public Set<ObixGroup> getNewMemberships() 
	{return Collections.unmodifiableSet(newMemberships);}
	
	public Set<ObixGroup> getDiscontinuedMemberships() 
	{return Collections.unmodifiableSet(discontinuedMemberships);}
}//end class def